package cn.lrwjz.geotest.adapter;

import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.lrwjz.geotest.R;

public class DeviceBluetoothAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<BluetoothDevice> devices;
    private OnItemClickListener listener;

    private int currentItem = 0;
    private boolean isClick = false;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(BluetoothDevice item, int position);
    }

    public BluetoothDevice getItem(int position) {
        return devices.get(position);
    }

    public void setCurrentItem(int currentItem){
        this.currentItem = currentItem;
    }

    public void setmIsClick(boolean isClick){
        this.isClick = isClick;
    }



    public DeviceBluetoothAdapter(List<BluetoothDevice> items) {
        this.devices = new ArrayList<>();
        if (items != null) {
            this.devices.addAll(items);
        }
    }

    public void clearData() {
        if (devices != null) {
            this.devices.clear();
            notifyDataSetChanged();
        }
    }

    public void addItem(BluetoothDevice item) {
        if (item != null && devices != null) {
            // 检查是否已存在相同地址的设备
            boolean exists = false;
            for (BluetoothDevice device : devices) {
                if (device.getAddress().equals(item.getAddress())) {
                    exists = true;
                    break;
                }
            }
            // 只有不存在的设备才添加
            if (!exists) {
                devices.add(item);
                notifyItemInserted(devices.size() - 1);
            }
        }
    }

    public List<BluetoothDevice> getDevices() {
        return devices;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_bluetooth, parent, false);
        return new ViewHolder(view);
    }

    private String getDeviceName(BluetoothDevice device, ViewHolder holder) {
        if (device == null) return "未知设备";
        
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                if (ActivityCompat.checkSelfPermission(holder.itemView.getContext(), 
                        Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                    return device.getAddress();
                }
            }
            String name = device.getName();
            return (name != null && !name.isEmpty()) ? name : device.getAddress();
        } catch (Exception e) {
            e.printStackTrace();
            return device.getAddress();
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (devices != null && position < devices.size()) {
            BluetoothDevice device = devices.get(position);
            ViewHolder viewHolder = (ViewHolder) holder;
            
            // 安全获取设备名称
            String deviceName = getDeviceName(device, viewHolder);
            viewHolder.tvName.setText(deviceName);

            // 使用资源引用设置背景色
            holder.itemView.setBackgroundColor(holder.itemView.getContext().getColor(R.color.white));

            if (currentItem == position && isClick) {
                holder.itemView.setBackgroundColor(holder.itemView.getContext().getColor(R.color.yellow));
            }

            // 设置点击事件
            if (listener != null) {
                holder.itemView.setOnClickListener(v -> listener.onItemClick(device, position));
            }
        }
    }

    @Override
    public int getItemCount() {
        return devices != null ? devices.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }
}
